import { BaseWindow } from "../../../../component/window/BaseWindow";
import { ILayer } from "../../../../mvc/interfaces/ILayer";
import { Language } from "../../../../../extend/language/Language";
import { Cache } from "../../../cache/Cache";
import { MsgManager } from "../../../manager/MsgManager";
import { MountConfig } from "../../../resource/MountConfig";
import { EColor } from "../../../../../Message/Public/EColor";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { PulseSharedObject } from "../../player/PulseSharedObject";
import { TodayNoTipsConst } from "../../../../component/gconst/TodayNoTipsConst";
import { CheckBoxWin } from "../../common/alertwins/CheckBoxWin";
import { DataEvent } from "../../../events/DataEvent";
import { GModuleEvent } from "../../../mvc/GModuleEvent";
import { ListEvent } from "../../../../../fl/events/ListEvent";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { ECategory } from "../../../../../Message/Public/ECategory";
import { DelegateGetSettingWindow } from "./DelegateGetSettingWindow";
import { ObjCreate } from "../../common/ObjCreate";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../../resource/ImagesConst";
import { RewordItemCellRenderer } from "../../copy/RewordItemCellRenderer";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { EAutoFeedType } from "../../../../../Message/Public/EAutoFeedType";
import { ToolTipUtil } from "../../common/tooltip/util/ToolTipUtil";
import { EBind } from "../../../../../Message/Public/EBind";
import { GTabBar } from "../../../../../com/mui/controls/GTabBar";
import { GTileList } from "../../../../../com/mui/controls/GTileList";
import { PageSelecter } from "../../common/pageSelect/PageSelecter";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { GTextFiled } from "../../../../../com/mui/controls/GTextFiled";
import { ALL } from "../../../../../this/ALL";
type int = number;
//class DelegateWarehouse
    
    export  class DelegateWarehouse extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            this._tabData = [{"name":"", "label": Language.getString(69679), "type": ALL}];
            this._items = [];
            super(arg1);
            this.setSize(435, 377);
            this.title = Language.getString(69645);
            return;
        }

        /* internal  */checkFeedItems(): Boolean
        {
            var loc2=null;
            if (!Cache.instance.pack.mountsPackCache.equipEdMount) 
            {
                MsgManager.showRollTipsMsg(Language.getString(20345));
                return false;
            }
            if (MountConfig.instance.isDtModel(Cache.instance.pack.mountsPackCache.equipEdMount.modelId) || Cache.instance.pack.mountsPackCache.isTimeliness(Cache.instance.pack.mountsPackCache.equipEdMount)) 
            {
                MsgManager.showRollTipsMsg(Language.getString(70264));
                return false;
            }
            var loc1=this.getFeedItems();
            var loc3=false;
            this._itemsFeedExp = 0;
            var loc4=0;
            var loc5=loc1;
            for(loc2 of loc5) 
            {
                this._itemsFeedExp = this._itemsFeedExp + loc2.itemInfo.item.mountExp * loc2.itemAmount;
                if (!(loc2.color >= EColor._EColorPurple)) 
                {
                    continue;
                }
                loc3 = true;
            }
            if (loc3) 
            {
                Alert.show(Language.getString(70153), Language.getString(20055), Alert.OK | Alert.CANCEL, null, this.onFeedMountHandler);
                return false;
            }
            return true;
        }

        /* internal  */onFeedMountHandler(arg1: int): void
        {
            if (arg1 != Alert.OK) 
            {
                return;
            }
            if (PulseSharedObject.isTodayNotTips(TodayNoTipsConst.FeedDelegateItemsTodayNoTips)) 
            {
                this.onFeedAlertClose(Alert.OK, false);
            }
            else 
            {
                Alert.alertWinRenderer = CheckBoxWin;
                Alert.show(Language.getStringByParam(69671, this._itemsFeedExp), Language.getString(69900), Alert.OK | Alert.CANCEL, null, this.onFeedAlertClose);
            }
            return;
        }

        /* internal  */checkFeedSkillItems(): Boolean
        {
            var loc2=null;
            if (!Cache.instance.role.suitSkillInfo || Cache.instance.role.suitSkillInfo.level <= 0 || Cache.instance.role.suitSkillInfo.skillId == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69703));
                return false;
            }
            var loc1=this.getFeedSkillItems();
            var loc3=false;
            this._itemsFeedSkillGas = 0;
            var loc4=0;
            var loc5=loc1;
            for(loc2 of loc5) 
            {
                this._itemsFeedSkillGas = this._itemsFeedSkillGas + loc2.gasValue * loc2.itemAmount;
                if (!(loc2.color >= EColor._EColorPurple)) 
                {
                    continue;
                }
                loc3 = true;
            }
            if (loc3) 
            {
                Alert.show(Language.getString(70153), Language.getString(20055), Alert.OK | Alert.CANCEL, null, this.onFeedSkillHandler);
                return false;
            }
            return true;
        }

        /* internal  */onFeedSkillHandler(arg1: int): void
        {
            if (arg1 != Alert.OK) 
            {
                return;
            }
            if (PulseSharedObject.isTodayNotTips(TodayNoTipsConst.FeedDelegateItemsTodayNoTips)) 
            {
                this.onFeedSkillAlertClose(Alert.OK, false);
            }
            else 
            {
                Alert.alertWinRenderer = CheckBoxWin;
                Alert.show(Language.getStringByParam(69704, this._itemsFeedSkillGas), Language.getString(69900), Alert.OK | Alert.CANCEL, null, this.onFeedSkillAlertClose);
            }
            return;
        }

        /* internal  */onDestroyAlertClose(arg1: int, arg2: Boolean): void
        {
            if (arg1 == Alert.OK) 
            {
                this.dispatchEvent(new DataEvent(GModuleEvent.DelegateWarehouse_destroyAll));
                this.hide();
            }
            if (arg1 == Alert.OK && arg2) 
            {
                PulseSharedObject.save(TodayNoTipsConst.DestroyDelegateItemsTodayNoTips, true);
            }
            return;
        }

        /* internal  */onFeedAlertClose(arg1: int, arg2: Boolean): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            if (arg1 == Alert.OK) 
            {
                loc1 = this.getFeedItems();
                loc2 = new Map<any, any> /* flash.utils.Dictionary */();
                var loc4=0;
                var loc5=loc1;
                for(loc3 of loc5) 
                {
                    loc2[loc3.uid] = loc3.itemAmount;
                }
                this.dispatchEvent(new DataEvent(GModuleEvent.DelegateWarehouse_FeedMount, loc2));
            }
            if (arg1 == Alert.OK && arg2) 
            {
                PulseSharedObject.save(TodayNoTipsConst.FeedDelegateItemsTodayNoTips, arg2);
            }
            return;
        }

        /* internal  */onFeedSkillAlertClose(arg1: int, arg2: Boolean): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            if (arg1 == Alert.OK) 
            {
                loc1 = this.getFeedSkillItems();
                loc2 = new Map<any, any> /* flash.utils.Dictionary */();
                var loc4=0;
                var loc5=loc1;
                for(loc3 of loc5) 
                {
                    loc2[loc3.uid] = loc3.itemAmount;
                }
                this.dispatchEvent(new DataEvent(GModuleEvent.DelegateWarehouse_FeedSuitSkill, loc2));
            }
            if (arg1 == Alert.OK && arg2) 
            {
                PulseSharedObject.save(TodayNoTipsConst.FeedDelegateItemsTodayNoTips, arg2);
            }
            return;
        }

        /* internal  */onItemClick(arg1: ListEvent): void
        {
            if (arg1.item) 
            {
                this.dispatchEvent(new DataEvent(GModuleEvent.DelegateWarehouse_Get, arg1.item));
            }
            return;
        }

        public updateItems(arg1: Array<any>): void
        {
            this._items = arg1;
            this.updateDataProvider();
            return;
        }

        /* internal  */onPageSelecterChange(arg1: Object /* flash.events.Event */): void
        {
            this.updateDataProvider();
            return;
        }

        /* internal  */updateDataProvider(): void
        {
            this._tabBar.selectedIndex = 0;
            if (!this._dataprovider) 
            {
                this._dataprovider = new DataProvider();
            }
            this._dataprovider.removeAll();
            var loc1=this._items;
            if (loc1 == null) 
            {
                loc1 = [];
            }
            this._pageSele.maxPage = Math.ceil(loc1.length / this._pageSize);
            var loc2=(this._pageSele.currentPage - 1) * this._pageSize;
            var loc3=this._pageSele.currentPage * this._pageSize > loc1.length ? loc1.length : this._pageSele.currentPage * this._pageSize;
            var loc4=loc2;
            while (loc4 < loc3) 
            {
                this._dataprovider.addItem(loc1[loc4]);
                ++loc4;
            }
            this._itemsList.dataProvider = this._dataprovider;
            return;
        }

        /* internal  */getFeedItems(): Array<any>
        {
            var loc2=null;
            var loc1=[];
            var loc3=0;
            var loc4=this._items;
            for(loc2 of loc4) 
            {
                if (!(loc2.itemInfo.item.mountExp > 0)) 
                {
                    continue;
                }
                loc1.push(loc2);
            }
            return loc1;
        }

        /* internal  */getFeedSkillItems(): Array<any>
        {
            var loc2=null;
            var loc1=[];
            var loc3=0;
            var loc4=this._items;
            for(loc2 of loc4) 
            {
                if (!(loc2.gasValue > 0)) 
                {
                    continue;
                }
                loc1.push(loc2);
            }
            return loc1;
        }

        /* internal  */getPropItems(): Array<any>
        {
            var loc2=null;
            var loc1=[];
            var loc3=0;
            var loc4=this._items;
            for(loc2 of loc4) 
            {
                if (loc2.category != ECategory._ECategoryProp) 
                {
                    continue;
                }
                loc1.push(loc2);
            }
            return loc1;
        }

        /* internal  */getEquipItems(): Array<any>
        {
            var loc2=null;
            var loc1=[];
            var loc3=0;
            var loc4=this._items;
            for(loc2 of loc4) 
            {
                if (loc2.category != ECategory._ECategoryEquip) 
                {
                    continue;
                }
                loc1.push(loc2);
            }
            return loc1;
        }

        public showSettingWin(): void
        {
            if (!this._getAllSettingWin) 
            {
                this._getAllSettingWin = new DelegateGetSettingWindow(this);
                this._getAllSettingWin.updateStatus();
            }
            this._getAllSettingWin.show();
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc4=0;
            var loc5=null;
            super.createChildren();
            ObjCreate.createBg(20, 63, 397, 225, this);
            this._tabBar = ObjCreate.createGTabBar(14, 34, this._tabData, 46, 22, this, this.onTypeTabChange);
            var loc1=0;
            while (loc1 < 10) 
            {
                loc4 = 0;
                while (loc4 < 5) 
                {
                    (loc5 = GlobalClass.getBitmap(ImagesConst.PackItemBg)).x = 24 + loc1 * 39;
                    loc5.y = 68 + loc4 * 39;
                    this.addChild(loc5);
                    ++loc4;
                }
                ++loc1;
            }
            this._itemsList = ObjCreate.createTileList(24, 68, 395, 200, this);
            this._itemsList.rowHeight = 38;
            this._itemsList.columnWidth = 38;
            this._itemsList.horizontalGap = 1;
            this._itemsList.verticalGap = 1;
            this._itemsList.setStyle("cellRenderer", RewordItemCellRenderer);
            this._itemsList.addEventListener(ListEvent.ITEM_CLICK, this.onItemClick);
            this.addChild(this._itemsList);
            ObjCreate.createGTextField(Language.getString(69682), 0, 263, 435, 20, this, new GTextFormat(FontUtil.defaultName, 12, GlobalStyle.color1Uint, null, null, null, null, null, flash.text.TextFormatAlign.CENTER));
            this._pageSele = ObjCreate.createPageSelecter(177, 293, this);
            this._pageSele.addEventListener(flash.events.Event.CHANGE, this.onPageSelecterChange);
            var loc2=ObjCreate.createLabel(Language.getString(69644), 92, 317, 300, 25, flash.text.TextFormatAlign.LEFT, this, true, GlobalStyle.color6Uint);
            loc2.x = (435 - loc2.textField.textWidth) / 2;
            var loc3=GlobalClass.getBitmap("Fire");
            loc3.x = loc2.x - 25;
            loc3.y = 310;
            this.addChild(loc3);
            this._getAllBtn = ObjCreate.createGButton(Language.getString(69642), 40, 291, 70, 26, this);
            this._getAllBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            this._feedMountBtn = ObjCreate.createGButton(Language.getString(69670), 144, 340, 70, 26, this);
            this._feedMountBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            this._feedSuitSkillBtn = ObjCreate.createGButton(Language.getString(69702), 235, 340, 70, 26, this);
            this._feedSuitSkillBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            this._getAndFeedBtn = ObjCreate.createGButton(Language.getString(71362), 40, 340, 88, 26, this);
            this._getAndFeedBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            this._getAndFeedBtn.toolTipData = Language.getString(71368);
            this._setLink = ObjCreate.createGTextField("", 290, 293, 100, 20, this);
            this._setLink.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._setLink.htmlText = HTMLUtil.addColor("<a href=\'event:\'><u>" + Language.getString(71361) + "</u></a>", GlobalStyle.color2);
            this._setLink.addEventListener(flash.events.TextEvent.LINK, this.onSetLinkClick);
            this._destroyAllBtn = ObjCreate.createGButton(Language.getString(69643), 330, 340, 70, 26, this);
            this._destroyAllBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            return;
        }

        /* internal  */onSetLinkClick(arg1): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.FeedMemoryModuleShow, 0));
            return;
        }

        /* internal  */onTypeTabChange(arg1): void
        {
            this.updateDataProvider();
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=false;
            var loc5=null;
            var loc6=null;
            var loc7=null;
            var loc8=0;
            var loc9=0;
            var loc10=arg1.target;
            switch (loc10) 
            {
                case this._getAllBtn:
                {
                    this.dispatchEvent(new DataEvent(GModuleEvent.DelegateWarehouse_GetAll, this._items));
                    break;
                }
                case this._feedMountBtn:
                {
                    loc1 = this.getFeedItems();
                    if (loc1.length > 0) 
                    {
                        if (this.checkFeedItems()) 
                        {
                            this.onFeedMountHandler(Alert.OK);
                        }
                    }
                    break;
                }
                case this._feedSuitSkillBtn:
                {
                    loc2 = this.getFeedSkillItems();
                    if (loc2.length > 0) 
                    {
                        if (this.checkFeedSkillItems()) 
                        {
                            this.onFeedSkillHandler(Alert.OK);
                        }
                    }
                    break;
                }
                case this._getAndFeedBtn:
                {
                    if (Cache.instance.feedMemory.sever) 
                    {
                        if (Cache.instance.feedMemory.sever.feedType != EAutoFeedType._EFeedTypeMount) 
                        {
                            if (Cache.instance.feedMemory.sever.feedType == EAutoFeedType._EFeedTypeAccumulation) 
                            {
                                if (!Cache.instance.role.suitSkillInfo || Cache.instance.role.suitSkillInfo.level <= 0 || Cache.instance.role.suitSkillInfo.skillId == 0) 
                                {
                                    MsgManager.showRollTipsMsg(Language.getString(69703));
                                    return;
                                }
                            }
                        }
                        else 
                        {
                            if (!Cache.instance.pack.mountsPackCache.equipEdMount) 
                            {
                                MsgManager.showRollTipsMsg(Language.getString(20345));
                                return;
                            }
                            if (MountConfig.instance.isDtModel(Cache.instance.pack.mountsPackCache.equipEdMount.modelId) || Cache.instance.pack.mountsPackCache.isTimeliness(Cache.instance.pack.mountsPackCache.equipEdMount)) 
                            {
                                MsgManager.showRollTipsMsg(Language.getString(70264));
                                return;
                            }
                        }
                        loc3 = Cache.instance.feedMemory.sever.memorys.concat();
                        loc4 = true;
                        loc5 = new Map<any, any> /* flash.utils.Dictionary */();
                        loc7 = [];
                        loc8 = 0;
                        while (loc8 < this._items.length) 
                        {
                            loc4 = true;
                            loc9 = 0;
                            while (loc9 < loc3.length) 
                            {
                                if (Cache.instance.feedMemory.isCanFeedByMemory(this._items[loc8], loc3[loc9])) 
                                {
                                    loc4 = false;
                                    loc7.push(this._items[loc8]);
                                    break;
                                }
                                ++loc9;
                            }
                            if (loc4) 
                            {
                                if (ToolTipUtil.isActiveIllustrateByItemdata(this._items[loc8]) && this._items[loc8].itemInfo.item.bind == EBind._EBindYes) 
                                {
                                    loc7.push(this._items[loc8]);
                                }
                                else 
                                {
                                    this.dispatchEvent(new DataEvent(GModuleEvent.DelegateWarehouse_Get, this._items[loc8]));
                                }
                            }
                            ++loc8;
                        }
                        if (loc7.length <= 0) 
                        {
                            return;
                        }
                        loc10 = 0;
                        var loc11=loc7;
                        for(loc6 of loc11) 
                        {
                            loc5[loc6.uid] = loc6.itemAmount;
                        }
                        if (Cache.instance.feedMemory.sever.feedType != EAutoFeedType._EFeedTypeMount) 
                        {
                            if (Cache.instance.feedMemory.sever.feedType == EAutoFeedType._EFeedTypeAccumulation) 
                            {
                                this.dispatchEvent(new DataEvent(GModuleEvent.DelegateWarehouse_FeedSuitSkill, loc5));
                            }
                        }
                        else 
                        {
                            this.dispatchEvent(new DataEvent(GModuleEvent.DelegateWarehouse_FeedMount, loc5));
                        }
                    }
                    break;
                }
                case this._destroyAllBtn:
                {
                    if (this._items.length > 0) 
                    {
                        if (PulseSharedObject.isTodayNotTips(TodayNoTipsConst.DestroyDelegateItemsTodayNoTips)) 
                        {
                            this.onDestroyAlertClose(Alert.OK, false);
                        }
                        else 
                        {
                            Alert.isWiggle = true;
                            Alert.alertWinRenderer = CheckBoxWin;
                            Alert.show(Language.getString(69648), Language.getString(69900), Alert.OK | Alert.CANCEL, null, this.onDestroyAlertClose);
                        }
                    }
                    break;
                }
            }
            return;
        }

        public /* const */ALL: string="ALL";

        public /* const */PROP: string="PROP";

        public /* const */EQUIP: string="EQUIP";

        /* internal */ /* var */_tabData: Array<any>;

        /* internal */ /* var */_tabBar: GTabBar;

        /* internal */ /* var */_items: Array<any>;

        /* internal */ /* var */_itemsList: GTileList;

        /* internal */ /* var */_pageSize: int=50;

        /* internal */ /* var */_pageSele: PageSelecter;

        /* internal */ /* var */_getAllBtn: GButton;

        /* internal */ /* var */_setLink: GTextFiled;

        /* internal */ /* var */_destroyAllBtn: GButton;

        /* internal */ /* var */_feedMountBtn: GButton;

        /* internal */ /* var */_feedSuitSkillBtn: GButton;

        /* internal */ /* var */_getAndFeedBtn: GButton;

        /* internal */ /* var */_getAllSettingWin: DelegateGetSettingWindow;

        /* internal */ /* var */_itemsFeedExp: int;

        /* internal */ /* var */_itemsFeedSkillGas: int;

        /* internal */ /* var */_dataprovider: DataProvider;
    }
